<?php
printf('<?xml version="1.0" encoding="UTF-8"?>');
?>

<rss xmlns:media="http://www.jztzp.com/rss/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0">  
  <channel> 
    <title>www.JZTZP.com 价值投资博客 - 首页</title>  
    <link>http://www.jztzp.com/rss/</link>  
    <description>价值投资博客每日精选, 博采诸多价值投资家之众长，汇聚价值投资的思想精华和研究数据。</description>  
    <language>zh-cn</language>  
    <copyright>www.JZTZP.com (C)</copyright>  
    <ttl>15</ttl>  
    <atom:link href="http://www.jztzp.com/feed/rss.xml" rel="self" type="application/rss+xml"/>  
<?php 
function TruncateStr($str, $max_len) {
    if (mb_strlen($str, 'utf-8') > $max_len) {
       return mb_substr($str, 0, $max_len - 1, 'utf-8') . '...';
    }
    return $str;
}

function GetDigest($aid) {
  $sql = sprintf("SELECT content FROM article_content WHERE id = %d", $aid);
  $result = mysql_query($sql);
  $row = mysql_fetch_array($result);
  if ($row) {
    // return TruncateStr(trim(str_replace('&nbsp;', '', strip_tags($row['content']))), 100);
    return $row['content'];
  }
  return null;
}

$count_per_page = 32;

$conn = mysql_connect("127.0.0.1","root","");
mysql_select_db("xingtan");

$sql=sprintf("SELECT a.id AS a_id, a.title AS a_title, a.digest AS a_digest, a.create_time AS a_create_time, 
    u.id AS u_id, u.name AS u_name, u.title AS u_title, u.url AS u_url 
    FROM articles AS a, authors AS u WHERE a.author = u.id AND a.category >= 0 ORDER BY a.id DESC limit %d", $count_per_page);
$result = mysql_query($sql);

while($row = mysql_fetch_array($result)) {
    printf('<item>');
    printf('<title>%s</title>', $row['a_title']);

    $desc = GetDigest($row['a_id']);
    if (!$desc) {
      $desc = $row['a_title'];
    }
    printf('<description><![CDATA[%s]]></description>', $desc);
    printf('<link>http://www.jztzp.com/a/%d.html</link>', $row['a_id']);
    printf('<guid isPermaLink="false">http://www.jztzp.com/a/%d.html</guid>', $row['a_id']);
    // printf('<pubDate>%s</pubDate>', strftime('%a, %d %b %Y %H:%M:%S %z', date('Y-m-d H:i:s', $row['a_create_time'])));
    printf('<pubDate>%s</pubDate>', $row['a_create_time']);
  printf('</item>');
}

mysql_close($conn);
?>
  </channel>
</rss>

